package org.feifei.user.controller;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.feifei.basic.exception.BusinessException;
import org.feifei.basic.util.CommonResult;
import org.feifei.user.constant.WxUserConstant;
import org.feifei.user.dto.LoginDTO;
import org.feifei.user.service.LoginInfoService;
import org.feifei.user.util.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author forever
 * @Description: 登录信息接口
 * @date 2021/12/09 16:29
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginInfoService loginInfoService;

    /**
     * 登录的方法
     * @param loginDTO 登录数据传输类
     * @return
     */
    @ApiModelProperty("登录的验证码方法")
    @PostMapping("/account")
    public CommonResult login(@RequestBody LoginDTO loginDTO) {
        try {
            return loginInfoService.login(loginDTO);
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure("登录失败 ：" + e.getMessage());
        }
    }

    /**
     *
     * @param map 微信扫码后获取的CODE
     * @return 返回 token 和 loginInfo 信息
     */
    @ApiModelProperty("微信登录的验证方法")
    @PostMapping("/wxaccount")
    public CommonResult WxUserlogin(@RequestBody Map<String, String> map) {
        try {
           return loginInfoService.WxUserLogin(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure("登录失败 ：" + e.getMessage());
        }
    }


    /**
     * 登录的方法
     * @param map 前端传过来的用户数据
     * @return
     */
    @ApiModelProperty("登录的验证码方法")
    @PostMapping("/binder/wechat")
    public CommonResult BinderWxUserETC(@RequestBody Map<String, String> map) {
        try {
            return loginInfoService.wxUserBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure("登录失败 ：" + e.getMessage());
        }
    }

    @ApiOperation("通过电话号码登录")
    @PostMapping("/phoneAccount")
    public CommonResult accountByPhone(Map<String, Object> map) {
        try {
            return loginInfoService.accountByPhone(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return CommonResult.failure(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failure("登录失败 ：" + e.getMessage());
        }

    }
}

